﻿using System;
using System.Runtime.Remoting.Messaging;

namespace Sofire.AOP
{
    class MethodCallSink : SinkBase
    {
        private MarshalByRefObject _obj;

        public MethodCallSink(MarshalByRefObject obj, IMessageSink nextSink)
            : base(nextSink)
        {
            this._obj = obj;
        }

        public override IMessage SyncProcessMessage(IMessage msg)
        {
            var methodCallMessage = msg as IMethodCallMessage;
            if(methodCallMessage == null) return base.SyncProcessMessage(msg);
            //MethodCall call = new MethodCall(
            var action = GetAction(methodCallMessage);
            if(!AOPSetting.EnterInvoke(this._obj, action, methodCallMessage)) return base.SyncProcessMessage(msg);
            var r = base.SyncProcessMessage(msg) as IMethodReturnMessage;

            AOPSetting.LeaveInvoke(this._obj, action, methodCallMessage, r);

            return r;
        }


    }
}
